package com.qdc.datacenters.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.qdc.datacenters.client.ChatMsgAuditClient;
import com.qdc.datacenters.client.CorpCustomerClient;
import com.qdc.datacenters.domain.entity.UserBehaviorData;
import com.qdc.datacenters.domain.query.chatmsg.ChatMsgListQuery;
import com.qdc.datacenters.domain.query.qw.QwBaseQuery;
import com.qdc.datacenters.domain.query.qw.QwMsgAuditQuery;
import com.qdc.datacenters.domain.query.qw.QwUserBehaviorDataQuery;
import com.qdc.datacenters.domain.vo.chatmsg.ChatMsgUserListVo;
import com.qdc.datacenters.domain.vo.qw.QwMsgAuditVo;
import com.qdc.datacenters.domain.vo.qw.QwUserBehaviorDataVo;
import com.qdc.datacenters.service.ICorpPermitUserService;
import com.qdc.datacenters.service.IUserBehaviorDataService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 员工统计任务
 * @author danmo
 * @date 2024-03-14 13:50
 **/

@Slf4j
@Component
public class UserBehaviorDataTask {

    @Resource
    private ChatMsgAuditClient chatMsgAuditClient;

    @Resource
    private CorpCustomerClient corpCustomerClient;

    @Autowired
    private IUserBehaviorDataService userBehaviorDataService;
    /**
     * 获取「联系客户统计」数据
     */
    @Async
    @Scheduled(cron = "0 0 5 * * ?")
    public void handler() {
        log.info("开始拉取【联系客户统计】数据");
        //时间范围
        Long startTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -1)).getTime() / 1000;
        Long endTime = DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -1)).getTime() / 1000;
        //获取会话内容存档开启成员列表
        QwMsgAuditQuery weBaseQuery = new QwMsgAuditQuery();
        QwMsgAuditVo permitUserList = chatMsgAuditClient.getPermitUserList(weBaseQuery);
        if (permitUserList != null && CollectionUtil.isNotEmpty(permitUserList.getIds())) {
            List<UserBehaviorData> dataList = new LinkedList<>();
            for (String userId : permitUserList.getIds()) {
                QwUserBehaviorDataQuery query = new QwUserBehaviorDataQuery();
                query.setStart_time(startTime);
                query.setEnd_time(endTime);
                query.setUserid(Collections.singletonList(userId));
                QwUserBehaviorDataVo qwUserBehaviorDataVo = corpCustomerClient.getUserBehaviorData(query);
                try {
                    List<QwUserBehaviorDataVo.BehaviorData> behaviorDataList = qwUserBehaviorDataVo.getBehaviorData();
                    for (QwUserBehaviorDataVo.BehaviorData data : behaviorDataList) {
                        UserBehaviorData weUserData = new UserBehaviorData();
                        BeanUtils.copyProperties(data, weUserData);
                        weUserData.setCorpUserId(userId);
                        weUserData.setReplyPercentage(String.valueOf(data.getReplyPercentage()));
                        dataList.add(weUserData);
                    }
                } catch (Exception e) {
                    log.error("员工数据拉取失败: userId:{}", userId, e);
                }
            }
            userBehaviorDataService.saveBatch(dataList);
        }
    }
}
